package com.oocl;

import java.io.IOException;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class TestLogger {
	public static void main(String[] args) throws SecurityException, IOException {
		Logger logger = Logger.getLogger("IMServer");
		logger.setLevel(Level.ALL);
		
		// write the log info to file by FileHandler
		FileHandler fileHandler = new FileHandler("logFile/log.log");
		fileHandler.setLevel(Level.ALL);
		fileHandler.setFormatter(new MyFormatter());
		
		//write log info to console by ConsoleHandler
		ConsoleHandler consoleHandler = new ConsoleHandler();
		consoleHandler.setLevel(Level.ALL);
		
		logger.addHandler(consoleHandler);
		logger.addHandler(fileHandler);
		
		// two way to write the log info
		logger.info("abc");
		logger.info("xyz");
		logger.log(Level.WARNING, "qwe");
	}
}


class MyFormatter extends Formatter{
	@Override
	public String format(LogRecord record) {	//format the info in the log file by self formatter
		return "["+record.getLevel()+"]\t"+new Date(record.getMillis())+"\t"
				+record.getSourceClassName()+"\n"+"\t\tLog Msg: "+record.getMessage()+"\n";
	}
	
}
